package controller

import (
	"affairs/param"
	"affairs/service"
	"affairs/tool"
	"fmt"
	"github.com/gin-gonic/gin"
	"strconv"
)

type UserController struct {

}

type register struct {

}



func (user *UserController) Router(engine *gin.Engine) {
	engine.GET("/info",user.getInfo)
	engine.POST("/login",user.userLogin)
}

/**
获取用户信息
 */
func (user *UserController) getInfo(ctx *gin.Context) {

	id,exist := ctx.GetQuery("id")
	if !exist {
		tool.Failed(ctx,"参数id获取失败")
		return
	}

	userId,err := strconv.Atoi(id)
	if err != nil {
		panic(err)
	}
	userService := service.UserService{}
	userInfo := userService.GetInfo(userId)
	if !userInfo {
		tool.Failed(ctx,"暂未获取到用户信息")
		return
	}
	tool.Success(ctx,"请求成功")
}

/*
手机号登录
 */
func (user *UserController) userLogin(ctx *gin.Context){
	var userLogin param.UserLoginParam

	err := tool.Decode(ctx.Request.Body,&userLogin)

	if err != nil {
		tool.Failed(ctx,"参数解析失败")
		return
	}
	userService := service.UserService{}
	userInfo := userService.UserLogin(userLogin)
	fmt.Println(userInfo)
	if userInfo != nil {
		tool.Success(ctx,userInfo)
		return
	}
	tool.Failed(ctx,"登录失败")
}
